package com.chang.controller;
import com.chang.pojo.Goods;
import com.chang.manage.Adminstrate;
import com.chang.service.GoodsService;
import com.chang.service.UserService;
import com.chang.menu.Menu;
import com.chang.pojo.User;

import java.io.IOException;
import java.util.Scanner;

public class CashSystem {
    Scanner scanner;//输入流
    //Alt+insert   (Alt+fn+insert)
    GoodsService goodsService;//声明服务层对象
    UserService userService;
    Menu menu;
    Adminstrate adminstrate;

    public CashSystem() throws InterruptedException, IOException {
        scanner = new Scanner(System.in);//实例化对象
        adminstrate = new Adminstrate();
        goodsService = new GoodsService();
        userService = new UserService();
        String o;
        do {
            menu.StartMenu();
            o = scanner.next();
            switch (o) {
                case "2": {
                    userService.addUser();
                }
                case "1": {
                    boolean oi=true;
                    String ts;
                    User user;
                    do {
                        System.out.print("请输入您的id:");
                        ts = scanner.next();
                        if (userService.Cashdb.get(ts) == null) {
                            System.out.println("您输入的账号有误，请重试");
                            oi = true;
                        } else {
                            oi = false;
                        }
                    } while (oi);
                    user = userService.Cashdb.get(ts);
                    if (userService.logonUser(user)) {
                        boolean tg;
                        String nr;
                        do {
                            menu.UserMenu();
                            nr = scanner.next();
                            tg = false;
                            switch (nr) {
                                case "1": {
                                    goodsService.purchase(user.getId());
                                    break;
                                }
                                case "2": {
                                    goodsService.searchOwnOeder(user.getId());
                                    break;
                                }
                                case "3": {
                                    userService.logoff(user.getId());
                                    break;
                                }
                                case "4": {
                                    System.out.println("退出系统");
                                    System.exit(0);
                                }
                                default: {
                                    System.out.println("出错了!");
                                }
                            }
                            String io;
                            String td;
                            do {
                                System.out.println("是否继续操作?(Y|N)");
                                td = scanner.next();
                                System.out.println("----------------");
                                if (!(td.equalsIgnoreCase("y") || td.equalsIgnoreCase("n"))) {
                                    System.out.println("您输入的内容有误，请重试!");
                                }
                            } while (!(td.equalsIgnoreCase("y") || td.equalsIgnoreCase("n")));
                            if (td.equalsIgnoreCase("y")) {
                                tg = true;
                            } else {
                                tg = false;
                                System.exit(0);
                            }
                        } while (tg);
                    } else {
                        System.exit(0);
                    }
                    break;
                }
                case "3": {
                    adminstrate.logonAdm();
                    boolean jx = true;
                    do {
                        menu.AdministrateMenu();
                        String kl = scanner.next();
                        switch (kl) {
                            case "1": {
                                String name;
                                int inventory;
                                double price;
                                goodsService.findAllGoods();
                                System.out.println("或者您还可以入库新的商品");
                                System.out.print("请您输入(新的)商品名称:");
                                name = scanner.next();
                                System.out.print("请您输入该商品的库存:");
                                inventory = scanner.nextInt();
                                System.out.print("请您输入该商品的售价:");
                                price = scanner.nextDouble();
                                Goods goods = new Goods();
                                goods.setGoodsName(name);
                                goods.setInventory(inventory);
                                goods.setPrice(price);
                                goodsService.updateGoods(goods);
                                break;
                            }
                            case "2": {
                                goodsService.deleteGoods();
                                break;
                            }
                            case "3": {
                                goodsService.findAllGoods();
                                goodsService.findGoods();
                                break;
                            }
                            case "4": {
                                boolean tg = false;
                                String id;
                                User user;
                                do {
                                    userService.showAllUser();
                                    System.out.print("请选择用户(id)");
                                    id = scanner.next();
                                    if (userService.Cashdb.get(id) == null) {
                                        System.out.println("该用户不存在，请重试!");
                                        tg = true;
                                    } else {
                                        System.out.println("用户选择成功");
                                        tg = false;
                                    }
                                } while (tg);
                                user = userService.Cashdb.get(id);
                                System.out.println(user.toString());
                                menu.manageUser();
                                String nr = scanner.next();
                                switch (nr) {
                                    case "1": {
                                        System.out.println("请输入您要更改的密码");
                                        String kwds = scanner.next();
                                        user.setKeywords(kwds);
                                        System.out.println("更改成功!");
                                        System.out.println("----------------");
                                        break;
                                    }
                                    case "2": {
                                        user.changeState();
                                        userService.writeUser();
                                        System.out.println("更改中...");
                                        Thread.sleep(1000);
                                        System.out.println("更改成功");
                                        if(user.getState()){
                                            System.out.println("用户账号解封");
                                        }else {
                                            System.out.println("用户账号已封禁");
                                        }
                                        break;
                                    }
                                    default: {
                                        System.out.println("您的输入有误，请重试");
                                    }
                                    case "3":
                                }
                                break;
                            }
                            case "5":{
                                goodsService.findAllTimes();
                                break;
                            }
                            case "6":{
                                goodsService.findAllInventory();
                                break;
                            }
                            case "7": {
                                System.exit(0);
                            }
                            default: {
                                System.out.println("您的输入有误，请重试");
                            }
                        }
                        boolean dx = false;
                        do {
                            System.out.println("是否继续?(Y|N)");
                            String po = scanner.next();
                            if (po.equalsIgnoreCase("y")) {
                                jx = true;
                                dx = false;
                            } else if (po.equalsIgnoreCase("n")) {
                                jx = false;
                                dx = false;
                            } else {
                                dx = true;
                            }
                        } while (dx);
                    } while (jx);
                    break;
                }
                case "4": {
                    System.exit(0);
                }
                default:
                    System.out.println("您的输入有误，请重试");
                    Thread.sleep(1000);
            }
        } while (o != "1" && o != "2" && o != "3" && o != "4");
    }
}
